home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / locks.c < prev    next >
C/C++ Source or Header  |  1996-02-11  |  3KB  |  158 lines

  1. /*
  2.  * locks.c
  3.  * Manage locking system
  4.  * This include the mutex's and cv's.
  5.  *
  6.  * Copyright (c) 1996 Systems Architecture Research Centre,
  7.  *           City University, London, UK.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  13.  */
  14.  
  15. #include <assert.h>
  16. #include "object.h"
  17. #include "baseClasses.h"
  18. #include "thread.h"
  19. #include "locks.h"
  20. #include "errors.h"
  21.  
  22. /*
  23.  * Lock a mutex.
  24.  */
  25. void
  26. lockMutex(object* obj)
  27. {
  28.     intsDisable();
  29.  
  30.     if (obj->holder == 0) {
  31.         obj->holder = currentThread;
  32.         obj->count = 1;
  33.     }
  34.     else if (obj->holder == currentThread) {
  35.         obj->count++;
  36.     }
  37.     else {
  38.         while (obj->holder != 0) {
  39.             suspendOnQThread(currentThread, &obj->muxWaiters);
  40.         }
  41.         obj->holder = currentThread;
  42.         obj->count = 1;
  43.     }
  44.  
  45.     intsRestore();
  46. }
  47.  
  48. /*
  49.  * Release a mutex.
  50.  */
  51. void
  52. unlockMutex(object* obj)
  53. {
  54.     thread* tid;
  55.  
  56.     intsDisable();
  57.  
  58.     assert(obj->holder == currentThread);
  59.  
  60.     obj->count--;
  61.     if (obj->count == 0) {
  62.         obj->holder = 0;
  63.         if (obj->muxWaiters != 0) {
  64.             tid = obj->muxWaiters;
  65.             obj->muxWaiters = tid->next;
  66.             resumeThread(tid);
  67.         }
  68.     }
  69.  
  70.     intsRestore();
  71. }
  72.  
  73. /*
  74.  * Wait on a conditional variable.
  75.  *  (timeout currently ignored)
  76.  */
  77. void
  78. waitCond(object* obj, long long timeout)
  79. {
  80.     int count;
  81.  
  82.     if (obj->holder != currentThread) {
  83.         throwException(IllegalMonitorStateException);
  84.     }
  85.  
  86.     intsDisable();
  87.  
  88.     count = obj->count;
  89.     obj->holder = 0;
  90.     obj->count = 0;
  91.  
  92.     /* Suspend, and keep suspended until I re-get the lock */
  93.     suspendOnQThread(currentThread, &obj->cvWaiters);
  94.     while (obj->holder != 0) {
  95.         suspendOnQThread(currentThread, &obj->muxWaiters);
  96.     }
  97.  
  98.     obj->holder = currentThread;
  99.     obj->count = count;
  100.  
  101.     intsRestore();
  102. }
  103.  
  104. /*
  105.  * Wake one thread on a conditional variable.
  106.  */
  107. void
  108. signalCond(object* obj)
  109. {
  110.     thread* tid;
  111.  
  112.     if (obj->holder != currentThread) {
  113.         throwException(IllegalMonitorStateException);
  114.     }
  115.  
  116.     intsDisable();
  117.  
  118.     /* Remove one thread from cv list */
  119.     if (obj->cvWaiters != 0) {
  120.         tid = obj->cvWaiters;
  121.         obj->cvWaiters = tid->next;
  122.  
  123.         /* Place it on mux list */
  124.         tid->next = obj->muxWaiters;
  125.         obj->muxWaiters = tid;
  126.     }
  127.  
  128.     intsRestore();
  129. }
  130.  
  131. /*
  132.  * Wake all threads on a conditional variable.
  133.  */
  134. void
  135. broadcastCond(object* obj)
  136. {
  137.     thread** tidp;
  138.  
  139.     if (obj->holder != currentThread) {
  140.         throwException(IllegalMonitorStateException);
  141.     }
  142.  
  143.     intsDisable();
  144.  
  145.     /* Find the end of the cv list */
  146.     if (obj->cvWaiters) {
  147.         for (tidp = &obj->cvWaiters; *tidp != 0; tidp = &(*tidp)->next)
  148.             ;
  149.  
  150.         /* Place entire cv list on mux list */
  151.         (*tidp) = obj->muxWaiters;
  152.         obj->muxWaiters = obj->cvWaiters;
  153.         obj->cvWaiters = 0;
  154.     }
  155.  
  156.     intsRestore();
  157. }
  158.